﻿using System;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_Cut_Problems
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Cut_Problems model)
        {
            string sqlstr;
            if (string.IsNullOrEmpty(model.Problem_end_time))
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + model.Changeover_id + "','" + model.Cute_problem_desc + "',NEWID()," + model.Cute_problem_type_id + ",'" + model.Problem_start_time + "',NULL);";
            else
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + model.Changeover_id + "','" + model.Cute_problem_desc + "',NEWID()," + model.Cute_problem_type_id + ",'" + model.Problem_start_time + "','" + model.Problem_end_time + "');";
            sqlstr = sqlstr + "declare @tmp int;select @tmp=count(cast(changeover_id as varchar(40))) from Table_Cut_Problems where changeover_id='" + model.Changeover_id + "' and problem_end_time is null;" +
                "if @tmp<>0 begin update Table_Changeover set is_problem=1 where changeover_id='" + model.Changeover_id + "'; end " +
                "else begin update Table_Changeover set is_problem=0 where changeover_id='" + model.Changeover_id + "'; end";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        public bool Insert(string changeover_id, string problem_type_id, string problem_desc, string problem_start_time, string problem_end_time)
        {
            string sqlstr;
            if (string.IsNullOrEmpty(problem_end_time))
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + changeover_id + "','" + problem_desc + "',NEWID()," + problem_type_id + ",'" + problem_start_time + "',NULL);";
            else
                sqlstr = "insert into Table_Cut_Problems(changeover_id,cute_problem_desc,cute_problem_id,cute_problem_type_id,problem_strat_time,problem_end_time) values (" +
                    "'" + changeover_id + "','" + problem_desc + "',NEWID()," + problem_type_id + ",'" + problem_start_time + "'," + problem_end_time + "');";
            sqlstr = sqlstr + "declare @tmp int;select @tmp=count(cast(changeover_id as varchar(40))) from Table_Cut_Problems where changeover_id='" + changeover_id + "' and problem_end_time is null;" +
                "if @tmp<>0 begin update Table_Changeover set is_problem=1 where changeover_id='" + changeover_id + "'; end " +
                "else begin update Table_Changeover set is_problem=0 where changeover_id='" + changeover_id + "'; end";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 更新
        public bool Update(Model_Table_Cut_Problems model)
        {
            string sql;
            if (string.IsNullOrEmpty(model.Problem_end_time))
                sql = "update Table_Cut_Problems set cute_problem_desc='" + model.Cute_problem_desc + "',cute_problem_type_id=" + model.Cute_problem_type_id + ",problem_strat_time='" +
                    model.Problem_start_time + "',problem_end_time=NULL where cute_problem_id='" + model.Cute_problem_id + "';";
            else
                sql = "update Table_Cut_Problems set cute_problem_desc='" + model.Cute_problem_desc + "',cute_problem_type_id=" + model.Cute_problem_type_id + ",problem_strat_time='" +
                    model.Problem_start_time + "',problem_end_time='" + model.Problem_end_time + "' where cute_problem_id='" + model.Cute_problem_id + "';";
            sql = sql + "declare @tmp int;select @tmp=count(cast(changeover_id as varchar(40))) from Table_Cut_Problems where changeover_id='" + model.Changeover_id + "' and problem_end_time is null;" +
                "if @tmp<>0 begin update Table_Changeover set is_problem=1 where changeover_id='" + model.Changeover_id + "'; end " +
                "else begin update Table_Changeover set is_problem=0 where changeover_id='" + model.Changeover_id + "'; end";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Cut_Problems GetData(string cute_problem_id)
        {
            string sql = "select * from Table_Cut_Problems where cute_problem_id='" + cute_problem_id + "'";
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Cut_Problems tmp;
                    tmp = new Model_Table_Cut_Problems(rdr["cute_problem_id"].ToString(), rdr["changeover_id"].ToString(), rdr["cute_problem_type_id"].ToString(),
                        rdr["cute_problem_desc"].ToString(), rdr["problem_strat_time"].ToString(), "");
                    if (rdr["problem_end_time"] != DBNull.Value)
                        tmp.Problem_end_time = rdr["problem_end_time"].ToString();
                    else
                        tmp.Problem_end_time = "";
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        {
            string sql = "UP_GET_PAGEINFO 'Table_Cut_Problems','*"
                     + "','" + condition + "','','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
            return dbhelper.Query(sql);
        }
        public System.Data.DataSet GetDatas(string condition)
        {
            string sqlstr = "select * from Table_Cut_Problems where " + condition;
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 删除记录
        public bool Delete(string fieldName, string fieldValue)
        {
            string sqlstr = "delete Table_Cut_Problems where " + fieldName + "=" + fieldValue;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 返回割接问题类型资料
        public System.Data.DataSet GetChangeoverProblemsType()
        {
            string sqlstr = "select changeover_problems_type_id,changeover_problems_type_name from Table_Changeover_problems_type";
            return dbhelper.Query(sqlstr);
        }
        #endregion
    }
}
